﻿<?php
//Schreibt die Nachrichten in die Datenbank ohne ein Formular und hängt diese gleich unten im Chat mit dran, ohne die Seite neu zu laden.
function mysqlConnect_PHP(){
	include('../config/config.inc.php');	
	$db = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
	$db_select = mysql_select_db($mysql_dbname, $db);	

}
function getDBrowID($userid, $dbname)
{
	if(empty($userid))
		return 0;
	else
	{
		mysqlConnect_PHP();
		$query = "SELECT * FROM ".$dbname." WHERE userid='".$userid."';";
		$result = mysql_query($query);
		while($res = mysql_fetch_array($result))
		{
			return $res;
		}	
	}
}
if(isset($_POST['msg']) && (isset($_POST['friend']) && (strlen($_POST['msg']) > 0) && (strlen($_POST['msg']) <=1000)))
{
	mysqlConnect_PHP();
	$save_msg = mysql_real_escape_string($_POST['msg']);
	$obj = new DateTime();
	$date = $obj->Format('d.m.Y');
	$time = $obj->Format('H:i:s');
	$result = mysql_query("set names 'utf8'"); 
	$mysql_query ="INSERT INTO message(`userid`, `friendid`, `date`, `time`, `unread`, `message`) values('".$_POST['userid']."', '".$_POST['friend']."', '".$date."', '".$time."', '1', '".$save_msg."');";
	mysql_query($mysql_query);
	$infos = getDBrowID($_POST['userid'], 'profile');
?>

	<div class="msg_box" value="self">
	<?php
		if(empty($infos['profilepic']))
			echo '<img src="img/nouser.png" alt="Fehler"/>';
		else
			echo '<img src="php/userimg/thumbnail_'.$infos['profilepic'].'" alt="Fehler"/>';
	?>
		<div class="message_name">
			<span><?php echo htmlspecialchars($infos['vorname']); ?></span>
			<span><?php echo htmlspecialchars($infos['nachname']); ?></span>
			<span value="user">[<?php echo htmlspecialchars($infos['username']); ?>]</span>
			<div class="message_date">
				<span><?php echo $time; ?></span>
				<span><?php echo $date; ?></span>
			</div>
		</div>
		<div class="msg_text">
			<?php echo stripslashes(nl2br(htmlspecialchars($_POST['msg']))); ?>
		</div>
	</div>
	<?php

}
?>